﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Yes.Core.Helper
{
    public class SecurityHelper
    {
        public static string Md5(string str)
        {
            byte[] b = Encoding.UTF8.GetBytes(str);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
            {
                ret = ret + b[i].ToString("x").PadLeft(2, '0');
            }
            return ret;
        }

        public static string Md5(Stream input)
        {
            using (MD5 md5 = MD5.Create())
            {
                byte[] hash = md5.ComputeHash(input);
                return BitConverter.ToString(hash).Replace("-", "").ToLower();
            }
        }

        public static string GetFileMd5(string filePath)
        {
            byte[] data = File.ReadAllBytes(filePath);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            string md5Str = BitConverter.ToString(result).Replace("-", "");
            return md5Str;
        }

        public static string GetFileMd5(byte[] data)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            string md5Str = BitConverter.ToString(result).Replace("-", "");
            return md5Str;
        }

        #region DES加密/解密

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="text">待加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string DesEncrypt(string text)
        {
            return DesEncrypt(text, "blog.yescent.com");
        }

        /// <summary> 
        /// DES加密 
        /// </summary> 
        /// <param name="text">待加密的字符串</param> 
        /// <param name="sKey">加密密钥</param> 
        /// <returns>加密后的字符串</returns> 
        public static string DesEncrypt(string text, string sKey)
        {
            byte[] inputArray = Encoding.UTF8.GetBytes(text);
            var tripleDES = TripleDES.Create();
            var byteKey = Encoding.UTF8.GetBytes(sKey);
            tripleDES.Key = byteKey;
            tripleDES.Mode = CipherMode.ECB;
            tripleDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tripleDES.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="text">待解密的字符串</param>
        /// <returns>解密后的字符串</returns>
        public static string DesDecrypt(string text)
        {
            return DesDecrypt(text, "blog.yescent.com");
        }

        /// <summary> 
        /// DES解密 
        /// </summary> 
        /// <param name="text">待解密的字符串</param> 
        /// <param name="sKey">解密密钥</param> 
        /// <returns>解密后的字符串</returns> 
        public static string DesDecrypt(string text, string sKey)
        {
            try
            {
                byte[] inputArray = Convert.FromBase64String(text);
                var tripleDES = TripleDES.Create();
                var byteKey = Encoding.UTF8.GetBytes(sKey);
                tripleDES.Key = byteKey;
                tripleDES.Mode = CipherMode.ECB;
                tripleDES.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = tripleDES.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
                return Encoding.UTF8.GetString(resultArray);

            }
            catch
            {
                return "";
            }
        }

        #endregion
    }
}
